package com.softer.chengxin.blacklist.dao;

import com.softer.chengxin.blacklist.dao.provider.BlacklistProvider;
import com.softer.chengxin.blacklist.domin.BlacklistVO;
import com.softer.chengxin.common.domin.Page;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;
import java.util.List;

/**
 * @author wangixnrui
 * @date 2018/10/12
 */
@Mapper
@Repository
public interface BlacklistDao {
    /**
     * 主管部门查询企业黑名单列表
     *
     * @param qyName      企业名
     * @param cxDateBegin 查询录入时间开始
     * @param cxDateEnd   查询录入时间结束
     * @param state       状态
     * @param area        该账号所管理的区域
     * @param page        页码
     * @return 结果
     */
    @SelectProvider(type = BlacklistProvider.class, method = "findBlacklistByCondition")
    List<BlacklistVO> findBlacklistByCondition(@Param("qyName") String qyName,
                                               @Param("cxDateBegin") String cxDateBegin,
                                               @Param("cxDateEnd") String cxDateEnd,
                                               @Param("state") Integer state,
                                               @Param("area") String area,
                                               @Param("page") Page page);

    /**
     * 主管部门查询企业黑名单列表 条数
     *
     * @param qyName      企业名
     * @param cxDateBegin 查询录入时间开始
     * @param cxDateEnd   查询录入时间结束
     * @param state       状态
     * @param area        该账号所管理的区域
     * @return 结果
     */
    @SelectProvider(type = BlacklistProvider.class, method = "findBlacklistByConditionCount")
    int findBlacklistByConditionCount(@Param("qyName") String qyName,
                                      @Param("cxDateBegin") String cxDateBegin,
                                      @Param("cxDateEnd") String cxDateEnd,
                                      @Param("state") Integer state,
                                      @Param("area") String area);

    /**
     * 新增企业黑名单
     *
     * @param blacklist 实体
     * @return 结果
     */
    @Insert("insert into company_blacklist(gmt_create,gmt_modified,company_id," +
            "score_type_id,adder,state,remark,file_id) values(now(),now()," +
            "#{b.companyId},#{b.scoreTypeId},#{b.adder},#{b.state},#{b.remark},#{b.fileId})")
    int add(@Param("b") BlacklistVO blacklist);

    /**
     * 查询黑名单表中是否存在该企业生效的信息
     *
     * @param corpId
     * @return
     */
    @Select("select count(*) from company_blacklist where state=1 and company_id=#{corpId}")
    int findCorpInfo(@Param("corpId") int corpId);

    /**
     * 移除黑名单
     *
     * @param blacklist 使用
     * @return 结果
     */
    @Update("update company_blacklist set gmt_modified=now(),state=#{b.state} " +
            " where id=#{b.id}")
    int remove(@Param("b") BlacklistVO blacklist);

    /**
     * 获取黑名单详情
     *
     * @param id 黑名单id
     * @return 结果
     */
    @Select("select c.`name` as qyName,b.id as id,DATE_FORMAT(b.gmt_create, '%Y-%m-%d') as gmtCreate,DATE_FORMAT(b.gmt_modified, '%Y-%m-%d') " +
            "as gmtModified,b.company_id as companyId,b.score_type_id AS scoreTypeId,(SELECT score_name FROM score_type WHERE id = b.score_type_id) AS scoreTypeName," +
            "m.name as adder,b.state as state,b.remark as remark,b.file_id as fileId " +
            "from company_blacklist b left join company_base_info c on c.id=b.company_id " +
            " left join manager_user m on m.login_name = b.adder where  b.id=#{id}")
    BlacklistVO getBlackDetail(@Param("id") Integer id);

    /**
     * 查询添加者
     *
     * @param id 黑名单Id
     * @return 添加入
     */
    @Select("select adder from company_blacklist where id=#{id}")
    String getAdder(@Param("id") Integer id);
}
